package com.jy.appmarket.fragment;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jy.appmarket.R;
import com.jy.appmarket.activity.ScreenShotsActivity;
import com.jy.appmarket.utils.UITools;
import com.jy.appmarket.view.ProgressWheel;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class FragmentScreenshot extends Fragment {
	//private static final String TAG = "FragmentScreenshot";

	public static final String EXTRA_SMALL = "small_url";
	public static final String EXTRA_NORMAL = "normal_url";
	public static final String EXTRA_POSITION = "position";
	public static final String EXTRA_SMALL_WIDTH = "width";
	public static final String EXTRA_SMALL_HEIGHT = "height";

	// private Activity mActivity;
	private ImageView mImageView;

	private ImageView mImageViewNormal;
	private ProgressWheel mProgressWheel;

	private String mSmall;
	private String mNormal;
	private int mPosition;

	private int mSmallWidth;
	private int mSmallHeight;
	private View mView = null;
	
	private Drawable mImageDrawable = null;

	@Override
	public void onAttach(Activity activity) {
		// mActivity = activity;
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

			mView = inflater.inflate(R.layout.screenshot_fragment, container,
					false);
			Bundle bundle = getArguments();
			if (bundle != null) {
				mSmall = bundle.getString(EXTRA_SMALL);
				mNormal = bundle.getString(EXTRA_NORMAL);
				mPosition = bundle.getInt(EXTRA_POSITION);

				mSmallWidth = bundle.getInt(EXTRA_SMALL_WIDTH);
				mSmallHeight = bundle.getInt(EXTRA_SMALL_HEIGHT);

			}
			initViews(mView);

		return mView;
	}

	private void initViews(View v) {

		mProgressWheel = v.findViewById(R.id.loading_progress);

		mImageView = v
				.findViewById(R.id.screenshot_fragment_imageview);
		ViewGroup.LayoutParams lp = mImageView.getLayoutParams();
		int[] size = calcAdaptionImageSize(mSmallWidth, mSmallHeight);
		if (size == null) {
			return;
		}

		lp.width = size[0];
		lp.height = size[1];
		mImageView.setLayoutParams(lp);

		if (ScreenShotsActivity.DrawableHolder.index == mPosition
				&& ScreenShotsActivity.DrawableHolder.drawable != null) {
			mImageView
					.setImageDrawable(ScreenShotsActivity.DrawableHolder.drawable);
			ScreenShotsActivity.DrawableHolder.index = -1;
			ScreenShotsActivity.DrawableHolder.drawable = null;
		} else {
			ImageLoader.getInstance().displayImage(mSmall, mImageView);
		}
		
		mImageDrawable = mImageView.getDrawable();
		
		mImageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (getActivity() != null) {
					((ScreenShotsActivity)getActivity()).activityFinish();
				}
			}
		});

		mImageViewNormal = v
				.findViewById(R.id.screenshot_fragment_imageview_normal);
		mImageViewNormal.setVisibility(View.GONE);
		mImageViewNormal.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (getActivity() != null) {
					((ScreenShotsActivity)getActivity()).activityFinish();
				}

			}
		});

		ImageLoader.getInstance().displayImage(mNormal, mImageViewNormal,
				new ImageLoadingListener() {

					@Override
					public void onLoadingStarted(String arg0, View arg1) {
						mProgressWheel.setVisibility(View.VISIBLE);
						mImageView.setVisibility(View.VISIBLE);
						mImageViewNormal.setVisibility(View.GONE);
					}

					@Override
					public void onLoadingFailed(String arg0, View arg1,
							FailReason arg2) {
						mProgressWheel.setVisibility(View.GONE);
						mImageView.setVisibility(View.VISIBLE);
						mImageViewNormal.setVisibility(View.GONE);
						
					}

					@Override
					public void onLoadingComplete(String arg0, View arg1,
							final Bitmap arg2) {
						mImageDrawable = mImageViewNormal.getDrawable();
						new Handler().postDelayed(new Runnable() { // 略微延迟，防止屏幕闪烁

									@Override
									public void run() {
										mProgressWheel.setVisibility(View.GONE);
										mImageView.setVisibility(View.GONE);
										mImageViewNormal
												.setVisibility(View.VISIBLE);

										int[] size = calcAdaptionImageSize(
												arg2.getWidth(),
                                                arg2.getHeight());
										if (size == null) {
											return;
										}

										ViewGroup.LayoutParams lp = mImageViewNormal
												.getLayoutParams();
										lp.width = size[0];
										lp.height = size[1];
										mImageViewNormal.setLayoutParams(lp);
									}
								}, 200);
					}

					@Override
					public void onLoadingCancelled(String arg0, View arg1) {
						// Do Nothing..
					}
				});

	}
	
	public Drawable getImageDrawable(){
		return mImageDrawable;
	}

	public static FragmentScreenshot newInstance(int position, String small,
			String nornal, int w, int h) {
		FragmentScreenshot f = new FragmentScreenshot();
		Bundle args = new Bundle();
		args.putString(EXTRA_SMALL, small);
		args.putString(EXTRA_NORMAL, nornal);
		args.putInt(EXTRA_POSITION, position);

		args.putInt(EXTRA_SMALL_WIDTH, w);
		args.putInt(EXTRA_SMALL_HEIGHT, h);

		f.setArguments(args);
		return f;
	}

	private int[] calcAdaptionImageSize(int w, int h) {
		int[] size = new int[2];

		if (getActivity() == null) {
			return null;
		}

		int[] screenSize = UITools.getDeviceScreenWidthHeight(getActivity());
		int screenW = screenSize[0];

		size[0] = screenW;
		size[1] = (int) ((float) h * ((float) screenW / (float) w));

		return size;

	}
}